package com.mvvmhabit.org.utils

import java.text.MessageFormat
import java.util.*

fun getTimeString(time: Long): String {
    var hour = 0
    var minute = 0
    var second = 0

    val timeTmp: Int = time.toInt()
    val hourTmp = timeTmp / 10000
    val minuteTmp = timeTmp % 10000 / 100
    val secondTmp = timeTmp % 100
    hour = getValue(hourTmp, hour, 0, 23)
    minute = getValue(minuteTmp, minute, 0, 59)
    second = getValue(secondTmp, second, 0, 59)
    return MessageFormat.format(
        "{0}:{1}:{2}",
        String.format("%02d", hour),
        String.format("%02d", minute),
        String.format("%02d", second)
    )
}

/**
 * 将时间转换为相关内容
 */
fun loadTimeByLong(time: Long): String {
    var data = time

    if (time.toString().length != 13) {
        data = time * 1000
    }
    val dataTime = Date(data)
    val min = dataTime.minutes
    var minString = "0"
    minString = if (min < 10) {
        "0$min"
    } else {
        min.toString()
    }
    val second = dataTime.seconds
    var secondString = "0"
    secondString = if (second < 10) {
        "0$second"
    } else {
        second.toString()
    }
    return "$minString:$secondString"
}


fun getValue(value: Int, def: Int, min: Int, max: Int): Int {
    return if (value in min..max) value else def
}